package cn.edu.cjw.APMFP.Resource.DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.ResourceBundle;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import cn.edu.cjw.APMFP.BOMTable.pojo.BOMTable;
import cn.edu.cjw.APMFP.Resource.Resource;
import cn.edu.cjw.APMFP.Util.HBaseUtill;
import cn.edu.cjw.APMFP.Util.HiveUtill;
import cn.edu.cjw.APMFP.Util.SelectExist;

public class ResourceDAOImpl implements ResourceDAO {

	public boolean addAndupdate(Resource resource) throws Exception {
		boolean flag = false;

		Connection conn = HBaseUtill.getConn();

		Table table = conn.getTable(TableName.valueOf(resource.RESOURCE_TABLE_NAME));

		if (SelectExist.selectExist(resource.RESOURCE_TABLE_NAME, "BId", resource.getBId())) {

			Result result = table.get(new Get(Bytes.toBytes(resource.getBId())));

			byte[] value = result.getValue("base_info".getBytes(), "housenum".getBytes());

			Long valueOf = Long.valueOf(new String(value));

			if (resource.getWareHouseNum() < 0) {

				if (-resource.getWareHouseNum() > valueOf) {
					return flag;
				}

			}
			valueOf += resource.getWareHouseNum();

			resource.setWareHouseNum(valueOf);
			Put putResource = HBaseUtill.putResource(resource);

			table.put(putResource);

			flag = true;

		} else {

			Put put = HBaseUtill.putResource(resource);

			table.put(put);

			flag = true;

		}

		HBaseUtill.Close(table, conn);
		return flag;
	}

	public boolean remove(String bid) throws Exception {
		boolean flag = false;

		Connection conn = HBaseUtill.getConn();

		Table table = conn.getTable(TableName.valueOf(Resource.RESOURCE_TABLE_NAME));

		Delete delete = new Delete(Bytes.toBytes(bid));

		table.delete(delete);

		HBaseUtill.Close(table, conn);

		flag = true;

		return flag;
	}

	public ArrayList<Resource> searchByName(String name) throws Exception {

		ArrayList<Resource> list = new ArrayList<Resource>();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String hive_url = bundle.getString("Hive-url");
		String hive_user = bundle.getString("Hive-user");
		String hive_password = bundle.getString("Hive-password");

		java.sql.Connection conn = HiveUtill.getConn(hive_url, hive_user, hive_password);

		String sql = "SELECT * FROM " + Resource.RESOURCE_TABLE_NAME + " WHERE BName=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, name);
		ResultSet rs = ps.executeQuery();

		while (rs.next()) {

			Resource resource = new Resource(Long.valueOf(rs.getString("housenum")), rs.getString("ChargeMan"));
			resource.ResetBOM(new BOMTable(rs.getString("BId"), rs.getString("BName"), rs.getString("Btype"),
					rs.getString("unit")));

			list.add(resource);
		}

		return list;

	}

	public Resource searchById(String id) throws Exception {

		Resource resource = new Resource();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String hive_url = bundle.getString("Hive-url");
		String hive_user = bundle.getString("Hive-user");
		String hive_password = bundle.getString("Hive-password");

		java.sql.Connection conn = HiveUtill.getConn(hive_url, hive_user, hive_password);

		String sql = "SELECT * FROM " + resource.RESOURCE_TABLE_NAME + " WHERE BId=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, id);
		ResultSet rs = ps.executeQuery();

		while (rs.next()) {

			resource = new Resource(Long.valueOf(rs.getString("housenum")), rs.getString("ChargeMan"));
			resource.ResetBOM(new BOMTable(rs.getString("BId"), rs.getString("BName"), rs.getString("Btype"),
					rs.getString("unit")));

		}

		return resource;
	}

	public ArrayList<Resource> searchByType(String type) throws Exception {

		ArrayList<Resource> list = new ArrayList<Resource>();

		ResourceBundle bundle = ResourceBundle.getBundle("System");
		String hive_url = bundle.getString("Hive-url");
		String hive_user = bundle.getString("Hive-user");
		String hive_password = bundle.getString("Hive-password");

		java.sql.Connection conn = HiveUtill.getConn(hive_url, hive_user, hive_password);

		String sql = "SELECT * FROM " + Resource.RESOURCE_TABLE_NAME + " WHERE Btype=?";
		PreparedStatement ps = conn.prepareStatement(sql);

		ps.setString(1, type);
		ResultSet rs = ps.executeQuery();

		while (rs.next()) {

			Resource resource = new Resource(Long.valueOf(rs.getString("housenum")), rs.getString("ChargeMan"));
			resource.ResetBOM(new BOMTable(rs.getString("BId"), rs.getString("BName"), rs.getString("Btype"),
					rs.getString("unit")));

			list.add(resource);
		}

		return list;
	}

}
